﻿@page "/Author"
@inject HttpClient _httpClient;
@inject IMessageService _message
@inject NavigationManager _NavigationManager
@using EmployeeLeaveSystem.Shared.Models
@using System.Net.Http.Headers;
@using System.Linq;
@inject ModalService _modalService;

<h3>我的个人信息</h3>
<div>
    <div class="avatar-class">
        <Avatar Style="height:100px;width:100px;box-shadow:rgb(107 135 181 / 35%) -5px 3px 6px 2px;" Src=@($"/ImgsReq/{myInfo?.Avatar_Name}") />
    </div>
    <div style="width:auto;height:100px;background-image:url('https://img0.baidu.com/it/u=2033734422,3304281792&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800');background-size:cover">
    </div>
    <div style="height:50px" />
</div>

<Descriptions Title="User Info" Bordered Layout="@DescriptionsLayout.Horizontal" Column="column">
    <DescriptionsItem Title="姓名">@myInfo?.Name</DescriptionsItem>
    <DescriptionsItem Title="性别">@myInfo?.StaffSex</DescriptionsItem>
    <DescriptionsItem Title="电话">@myInfo?.Phone_Number</DescriptionsItem>
    <DescriptionsItem Title="国家">@myInfo?.Nation</DescriptionsItem>
    <DescriptionsItem Title="身份证">@myInfo?.ID_Card</DescriptionsItem>
    <DescriptionsItem Title="个性签名" Span="4">@myInfo?.Description</DescriptionsItem>
    <DescriptionsItem Title="个人状态" Span="3">
        <Badge Status="processing" Text="@myInfo?.StaffState.ToString()"></Badge>
    </DescriptionsItem>
    <DescriptionsItem Title="所属部门信息">
        @myInfo?.Department?.Name
    </DescriptionsItem>
</Descriptions>
<br />
<Button OnClick="()=>{OpenModalUpdate(GobalManager.myInfo.Id);}">去修改我的信息</Button>
<Button OnClick="()=>{OnInitializedAsync();}">刷新</Button>
@{
    String _title = "退出", _body = "您确定要退出登录吗?";
}
<Button Danger OnClick="()=>{
    _modalService.Confirm(new(){Title=_title,Content=_body,OnOk=async (e)=>{
GobalManager.myInfo=null;
GobalManager.FlushPages();
await InvokeAsync(StateHasChanged);
}});
}">
    退出登录
</Button>
<br />

@code {
    public static int QueryId { get; set; }
    bool shouldRender = false;
    protected override bool ShouldRender() => shouldRender;
    StaffInfo? myInfo { get; set; }
    string AvatarUrl { get; set; } = "";
    protected override void OnInitialized() {
    }
    protected override async Task OnInitializedAsync() {
        if (GobalManager.myInfo != null) Author.QueryId = GobalManager.myInfo.Id;
        myInfo = await _httpClient.GetFromJsonAsync<StaffInfo>($"/Main/GetStaffInfoById?id={QueryId}");
        shouldRender = true;
        GobalManager.myInfo = myInfo;
        await base.OnInitializedAsync();
    }
    private bool isLoading;

    private async void LoadFiles(InputFileChangeEventArgs e) {
        var file = e.GetMultipleFiles(1).FirstOrDefault();
        Console.WriteLine("上传的文件名：" + file.Name);

        using var content = new MultipartFormDataContent(); // FormData

        var fileContent = new StreamContent(file.OpenReadStream()); // 打开文件进入读取

        fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType); // 编写文件类型

        content.Add(content: fileContent,
    name: "\"files\"",
    fileName: file.Name); // 加入content中MultipartFormDataContent

        var response = await _httpClient.PostAsync(@"api/File/UpLoadFile", content);
        var resultStr = await response.Content.ReadAsStringAsync();
        AvatarUrl = resultStr;
        shouldRender = true;
        InvokeAsync(StateHasChanged);
    }
    //https://learn.microsoft.com/zh-cn/aspnet/core/blazor/file-uploads?view=aspnetcore-7.0&pivots=webassembly

    private Dictionary<string, int> column = new Dictionary<string, int> {
            { "xxl", 3 },
            { "xl", 3},
            { "lg", 2},
            { "md", 2},
            { "sm", 1},
            { "xs", 1}
                                                        };


    private async Task OpenModalUpdate(int Id) {
        var modalConfig = new ModalOptions {
                    Title = "详情",
                    Draggable = true,
                    Footer = null,
                    Maximizable = true,
                    Width = "80vw",
                    Content = @<UpdateAuthor Id="Id" />
        };

        await _modalService.CreateModalAsync(modalConfig);
    }
}
